package com.example.myui.activity;

import android.app.Notification;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.example.myui.R;
import com.sunfusheng.glideimageview.GlideImageLoader;
import com.sunfusheng.glideimageview.GlideImageView;
import com.sunfusheng.glideimageview.progress.CircleProgressView;
import com.sunfusheng.glideimageview.progress.OnGlideImageViewListener;

public class GlideActivity extends AppCompatActivity {

    private CircleProgressView progressView2;
    public static boolean isLoadAgain = false; // Just for fun when loading images!
    public static final String KEY_IMAGE_URL = "image_url";
    public static final String KEY_IMAGE_URL_THUMBNAIL = "image_url_thumbnail";
    private String cat = "https://raw.githubusercontent.com/sfsheng0322/GlideImageView/master/screenshot/cat.jpg";
    private GlideImageView glideview3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glide);
        initView();
    }

    private void initView() {
        GlideImageView glideview1 = findViewById(R.id.glideview1);
        GlideImageView glideview2 = findViewById(R.id.glideview2);
        glideview3 = findViewById(R.id.glideview3);
        progressView2 = findViewById(R.id.progressView2);

        glideview1.setCircle(true);
        glideview1.setBorderColor(Color.RED);
        glideview1.setBorderWidth(1);
        glideview1.loadCircleImage(cat, R.mipmap.ic_launcher);

        glideview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GlideActivity.this, SingleImageActivity.class);
                intent.putExtra(KEY_IMAGE_URL, cat);
                intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, cat);
                ActivityOptionsCompat compat = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(GlideActivity.this, glideview3, getString(R.string.transitional_image));
                ActivityCompat.startActivity(GlideActivity.this, intent, compat.toBundle());
            }
        });
        glideview3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GlideActivity.this, SingleImageActivity.class);
                intent.putExtra(KEY_IMAGE_URL, cat);
                intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, cat);
                ActivityOptionsCompat compat = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(GlideActivity.this, glideview3, getString(R.string.transitional_image));
                ActivityCompat.startActivity(GlideActivity.this, intent, compat.toBundle());
            }
        });


//        RequestOptions requestOptions = glideview2.requestOptions(R.color.placeholder).centerCrop();
//
//        // 第二种方式加载：可以解锁更多功能
//        GlideImageLoader imageLoader = glideview2.getImageLoader();
//        imageLoader.setOnGlideImageViewListener(cat, (percent, isDone, exception) -> {
//            if (exception != null && !TextUtils.isEmpty(exception.getMessage())) {
//                Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
//            }
//            progressView2.setProgress(percent);
//            progressView2.setVisibility(isDone ? View.GONE : View.VISIBLE);
//        });
//        imageLoader.requestBuilder(cat, requestOptions)
//                .transition(DrawableTransitionOptions.withCrossFade())
//                .into(glideview2);

    }

    public void doGlide(View view) {
        Intent intent = new Intent(GlideActivity.this, SingleImageActivity.class);
        intent.putExtra(KEY_IMAGE_URL, cat);
        intent.putExtra(KEY_IMAGE_URL_THUMBNAIL, cat);
        ActivityOptionsCompat compat = ActivityOptionsCompat
                .makeSceneTransitionAnimation(GlideActivity.this, glideview3, getString(R.string.transitional_image));
        ActivityCompat.startActivity(GlideActivity.this, intent, compat.toBundle());
    }

//    private void loadImage(String image_url_thumbnail, String image_url,GlideImageView glideImageView) {
//        RequestOptions requestOptions = glideImageView.requestOptions(R.color.black)
//                .centerCrop()
//                .skipMemoryCache(true) // 跳过内存缓存
//                .diskCacheStrategy(DiskCacheStrategy.NONE); // 不缓存到SDCard中
//
//        glideImageView.getImageLoader().setOnGlideImageViewListener(image_url, new OnGlideImageViewListener() {
//            @Override
//            public void onProgress(int percent, boolean isDone, GlideException exception) {
//                progressView2.setProgress(percent);
//                progressView2.setVisibility(isDone ? View.GONE : View.VISIBLE);
//            }
//        });
//
//        glideImageView.getImageLoader().requestBuilder(image_url, requestOptions)
//                .thumbnail(Glide.with(GlideActivity.this) // 加载缩略图
//                        .load(image_url_thumbnail)
//                        .apply(requestOptions))
//                .transition(DrawableTransitionOptions.withCrossFade()) // 动画渐变加载
//                .into(glideImageView);
//    }
}
